import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/shared/util/util_mk.dart';
import 'package:bxultimate/widgets/app/app_input.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:bxultimate/widgets/mk_scroll_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart' hide TDInput, TDInputType;

import 'controller.dart';

class OrganizationAttendanceCreatePage extends StatefulWidget {
  const OrganizationAttendanceCreatePage({Key? key}) : super(key: key);

  @override
  State<OrganizationAttendanceCreatePage> createState() =>
      _OrganizationAttendanceCreatePageState();
}

class _OrganizationAttendanceCreatePageState
    extends State<OrganizationAttendanceCreatePage> {
  final controller = Get.find<OrganizationAttendanceCreateController>();
  final state = Get.find<OrganizationAttendanceCreateController>().state;

  @override
  Widget build(BuildContext context) {
    return MkPage(
        child: Scaffold(
      backgroundColor: Colors.transparent,
      appBar: TDNavBar(
        backgroundColor: Colors.transparent,
        title: '创建考勤',
      ),
      body: MkScrollView(
        bottomButton: TDButton(
          text: '确认创建',
          shape: TDButtonShape.circle,
          height: 48,
          onTap: controller.publish,
          isBlock: true,
          theme: TDButtonTheme.primary,
        ),
        children: [
          Gaps.vGap16,
          TDInput(
            controller: controller.titleController,
            leftLabel: '标题',
            hintText: '请输入标题',
            required: true,
            type: TDInputType.special,
            textAlign: TextAlign.end,
            backgroundColor: Colors.transparent,
          ),
          Obx(() => TDCell(
                style: TDCellStyle(
                    backgroundColor: Colors.transparent,
                    requiredStyle: TextStyle(
                      color: TDTheme.of(context).errorColor6,
                      fontSize: TDTheme.of(context).fontBodyLarge?.size ?? 16,
                      height: TDTheme.of(context).fontBodyLarge?.height ?? 24,
                      fontWeight: FontWeight.w400,
                    )),
                arrow: true,
                required: true,
                noteWidget: _noteWidget(
                    '${state.createDto.taskStartTime == null ? '请选择' : state.createDto.taskStartTime}'),
                title: '开始时间',
                onClick: (TDCell d) {
                  controller.showUpTimeSelect(1);
                },
              )),
          Obx(() => TDCell(
                style: TDCellStyle(
                    backgroundColor: Colors.transparent,
                    requiredStyle: TextStyle(
                      color: TDTheme.of(context).errorColor6,
                      fontSize: TDTheme.of(context).fontBodyLarge?.size ?? 16,
                      height: TDTheme.of(context).fontBodyLarge?.height ?? 24,
                      fontWeight: FontWeight.w400,
                    )),
                arrow: true,
                required: true,
                noteWidget: _noteWidget(
                    '${state.createDto.taskEndTime == null ? '请选择' : state.createDto.taskEndTime}'),
                title: '结束时间',
                onClick: (TDCell d) {
                  controller.showUpTimeSelect(2);
                },
              )),
          Obx(() => TDCell(
                style: TDCellStyle(
                    backgroundColor: Colors.transparent,
                    requiredStyle: TextStyle(
                      color: TDTheme.of(context).errorColor6,
                      fontSize: TDTheme.of(context).fontBodyLarge?.size ?? 16,
                      height: TDTheme.of(context).fontBodyLarge?.height ?? 24,
                      fontWeight: FontWeight.w400,
                    )),
                arrow: true,
                required: true,
                noteWidget: state.addressName != ''
                    ? _noteWidget('${state.addressName}')
                    : _noteWidget('请选择地理位置'),
                description: state.addressName == '' ? '' : state.addressName,
                title: '地点',
                onClick: controller.addressSelect,
              )),
          TDInput(
            controller: controller.distanceController,
            leftLabel: '范围',
            required: true,
            inputType: TextInputType.number,
            type: TDInputType.special,
            textAlign: TextAlign.end,
            backgroundColor: Colors.transparent,
          ),
          Obx(() => TDCell(
                style: TDCellStyle(
                    backgroundColor: Colors.transparent,
                    requiredStyle: TextStyle(
                      color: TDTheme.of(context).errorColor6,
                      fontSize: TDTheme.of(context).fontBodyLarge?.size ?? 16,
                      height: TDTheme.of(context).fontBodyLarge?.height ?? 24,
                      fontWeight: FontWeight.w400,
                    )),
                arrow: true,
                required: true,
                noteWidget: _noteWidget('已选择${state.idcardList.length}人'),
                title: '参与人员',
                onClick: controller.toSelectUser,
              )),
          Obx(() => TDCell(
                style: TDCellStyle(
                    backgroundColor: Colors.transparent,
                    requiredStyle: TextStyle(
                      color: TDTheme.of(context).errorColor6,
                      fontSize: TDTheme.of(context).fontBodyLarge?.size ?? 16,
                      height: TDTheme.of(context).fontBodyLarge?.height ?? 24,
                      fontWeight: FontWeight.w400,
                    )),
                arrow: true,
                required: true,
                noteWidget: _noteWidget('${state.clockingType}'),
                title: '打卡方式',
                onClick: controller.attendanceTypeSelect,
              )),

          // 自定义
          TDButton(
            text: '考勤规则设置',
            isBlock: true,
            theme: TDButtonTheme.light,
            onTap: addCustomClocking,
          ),

          Obx(() => _buildCustomRow()),

          TDInput(
            controller: controller.remarkController,
            leftLabel: '备注',
            type: TDInputType.special,
            textAlign: TextAlign.end,
            backgroundColor: Colors.transparent,
          ),
        ],
      ),
    ));
  }

  _buildCustomRow() {
    return state.createDto.attendanceClockingTimeList == null
        ? SizedBox()
        : Column(
      children: [
        for (var i = 0;
        i < state.createDto.attendanceClockingTimeList!.length;
        i++)
          TDCell(
            rightIconWidget: GestureDetector(
              onTap: () {
                controller.removeClockingTimeList(i);
              },
              child: Icon(Icons.close),
            ),
            rightIcon: Icons.close,
            style: TDCellStyle(
                backgroundColor: Colors.transparent,
                descriptionStyle:
                TextStyle(color: Color(0xFF606266), fontSize: 14)),
            description:
            '考勤时间: ${state.createDto.attendanceClockingTimeList![i].startTime} 至 ${state.createDto.attendanceClockingTimeList![i].endTime}',
            title: state.createDto.attendanceClockingTimeList![i].label,
            onClick: controller.attendanceTypeSelect,
          )
      ],
    );
  }

  addCustomClocking() {

    if(state.createDto.clockType == 0 && state.createDto.attendanceClockingTimeList!.length == 1){
      SmartDialog.showNotify(msg: '您已完成设置', notifyType: NotifyType.failure);
      return;
    }

    UtilMk.openBottomSheet(
        300,
        '添加考勤规则',
        Column(
          children: [
            TDInput(
              controller: controller.customTitleController,
              leftLabel: '标题',
              required: true,
              type: TDInputType.special,
              textAlign: TextAlign.end,
              backgroundColor: Colors.transparent,
            ),
            Obx(() => TDCell(
                  style: TDCellStyle(
                      backgroundColor: Colors.transparent,
                      requiredStyle: TextStyle(
                        color: TDTheme.of(context).errorColor6,
                        fontSize: TDTheme.of(context).fontBodyLarge?.size ?? 16,
                        height: TDTheme.of(context).fontBodyLarge?.height ?? 24,
                        fontWeight: FontWeight.w400,
                      )),
                  arrow: true,
                  required: true,
                  noteWidget: _noteWidget('${state.customStartTime}'),
                  title: '开始时间',
                  onClick: (TDCell d) {
                    controller.showCustomTimeSelect(1);
                  },
                )),
            Obx(() => TDCell(
                  style: TDCellStyle(
                      backgroundColor: Colors.transparent,
                      requiredStyle: TextStyle(
                        color: TDTheme.of(context).errorColor6,
                        fontSize: TDTheme.of(context).fontBodyLarge?.size ?? 16,
                        height: TDTheme.of(context).fontBodyLarge?.height ?? 24,
                        fontWeight: FontWeight.w400,
                      )),
                  arrow: true,
                  required: true,
                  noteWidget: _noteWidget('${state.customEndTime}'),
                  title: '结束时间',
                  onClick: (TDCell d) {
                    controller.showCustomTimeSelect(2);
                  },
                )),
            TDButton(
              text: '添加',
              isBlock: true,
              theme: TDButtonTheme.light,
              onTap: controller.addCustom,
            ),
          ],
        ));
  }

  _noteWidget(text) {
    return Text(
      text,
      style: TextStyle(fontSize: 16),
    );
  }

  @override
  void dispose() {
    Get.delete<OrganizationAttendanceCreateController>();
    super.dispose();
  }
}
